import java.io.File;

public class SeMaLyMain
{
	public static DataAnalyser	dataAnalyser;
	public static int			tickets					= 16;								// parallel purifying tasks

	public static boolean		SPLIT					= false;							// split server.out files to avoid out of memory exception
	public static boolean		EXTRACT					= false;							// extract data from server.out (split) files
	public static boolean		PURIFY					= false;							// enrich with DB informations (approximate 7 hours for
																							// one 2 GB server.out file)
	public static boolean		STATISTICS				= false;							// analysis over extracted data
	public static boolean		TEST_FILE				= true;							// file to test data

	public static String		SERVER_OUT_FOLDER		= "Test/";
	public static String		EXTRACTED_XML_FOLDER	= "xml/";
	public static String		PURIFYED_XML_FOLDER		= "pury/";
	public static String		TEST_FILE_NAME			= "ServerOutTest.out";
	public static String		SPLIT_FILE_PATH			= "E:/Toni/workspace/SeMaLytics/";
	public static String		SPLIT_FILE_NAME			= "server.out.1";

	/**
	 * Create needed folders
	 */
	private static void prepareFolder()
	{
		new File(SERVER_OUT_FOLDER).mkdir();
		new File(SERVER_OUT_FOLDER + EXTRACTED_XML_FOLDER).mkdir();
		new File(SERVER_OUT_FOLDER + EXTRACTED_XML_FOLDER + PURIFYED_XML_FOLDER).mkdir();
		new File(SERVER_OUT_FOLDER + EXTRACTED_XML_FOLDER + PURIFYED_XML_FOLDER.replaceAll("/", "") + "_test/").mkdir();
	}

	/**
	 * Main Node, starts all processes
	 * 
	 * @param args Command line input param
	 */
	public static void main(String[] args)
	{
		prepareFolder();

		if (SPLIT)
		{
			DataSplitter splitter = new DataSplitter(SPLIT_FILE_PATH, SPLIT_FILE_NAME);
		}

		File dir = new File(SERVER_OUT_FOLDER);
		File[] fileList = dir.listFiles();

		if (EXTRACT || PURIFY)
		{
			for (File f : fileList)
			{
				if (f.isFile())
				{
					dataAnalyser = new DataAnalyser();
					DataExtractor extractor = new DataExtractor();

					if (EXTRACT)
						extractor.proceedData(SERVER_OUT_FOLDER, f.getName());

					if (PURIFY)
					{
						while (tickets <= 0)
						{
							try
							{
								Thread.sleep(5000);
							}
							catch (InterruptedException e)
							{
								e.printStackTrace();
							}
						}

						if (tickets > 0)
						{
							tickets--;
							Thread parallelPuryfying = new ParallelPuryfying(SERVER_OUT_FOLDER + EXTRACTED_XML_FOLDER, f.getName() + ".xml", SERVER_OUT_FOLDER + EXTRACTED_XML_FOLDER
									+ PURIFYED_XML_FOLDER, f.getName() + "_pury");
							parallelPuryfying.start();
						}
					}

					System.gc();
				}
			}
		}

		if (STATISTICS)
		{
			Statistics stats = new Statistics();
			stats.evaluateData(SERVER_OUT_FOLDER + EXTRACTED_XML_FOLDER + PURIFYED_XML_FOLDER);
		}

		if (TEST_FILE)
		{
			dataAnalyser = new DataAnalyser();
			
			DataExtractor extractor = new DataExtractor();
			extractor.proceedData(SERVER_OUT_FOLDER, TEST_FILE_NAME);

			DataPurifyer purifyer = new DataPurifyer();
			purifyer.proceedData(SERVER_OUT_FOLDER + EXTRACTED_XML_FOLDER, TEST_FILE_NAME + ".xml", SERVER_OUT_FOLDER + EXTRACTED_XML_FOLDER + PURIFYED_XML_FOLDER.replaceAll("/", "") + "_test/", TEST_FILE_NAME
					+ "_pury");

			Statistics stats = new Statistics();
			stats.evaluateData(SERVER_OUT_FOLDER + EXTRACTED_XML_FOLDER + PURIFYED_XML_FOLDER.replaceAll("/", "") + "_test/");
		}

	}
}
